<%@page import="tu.java.login.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.Vector, tu.java.projection.Projection, tu.java.utils.UIUtils, tu.java.movie.Movie, tu.java.hall.Hall"%>
    
<%@include file="template/header.jspf" %>

<%
if(!Menu.checkUserAcess(request, 5)){
	response.sendRedirect(request.getContextPath());
	return;
}
User user = (User)session.getAttribute("userObj");
int userId = user.getUserId();

if (request.getParameter("mode") == null) {
	response.sendRedirect("projections.jsp?mode=search");
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("save")) {
	Projection projection = Projection.searchProjections(0, 0, null, null, Integer.parseInt(request.getParameter("id"))).get(0);
	projection.setMovie(Movie.searchMovies("", Integer.parseInt(request.getParameter("movie"))).get(0));
	projection.setHall(Hall.searchHalls("", Integer.parseInt(request.getParameter("hall"))).get(0));
	projection.setProjectionBeginDateTime(request.getParameter("fromDate") + " " + request.getParameter("fromTime") + ":00");
	projection.setProjectionEndDateTime(request.getParameter("toDate") + " " + request.getParameter("toTime") + ":00");
	projection.editProjection(userId);
	response.sendRedirect("projections.jsp?mode=edit&id=" + request.getParameter("id"));
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("addproj")) {
	Projection projection = new Projection();
	projection.setMovie(Movie.searchMovies("", Integer.parseInt(request.getParameter("movie"))).get(0));
	projection.setHall(Hall.searchHalls("", Integer.parseInt(request.getParameter("hall"))).get(0));
	projection.setProjectionBeginDateTime(request.getParameter("fromDate") + " " + request.getParameter("fromTime") + ":00");
	projection.setProjectionEndDateTime(request.getParameter("toDate") + " " + request.getParameter("toTime") + ":00");
	projection.addProjection(userId);
	response.sendRedirect("projections.jsp?mode=search");
}

if (request.getParameter("mode") != null && request.getParameter("mode").equals("delete")) {
	Projection projection = Projection.searchProjections(0, 0, null, null, Integer.parseInt(request.getParameter("id"))).get(0);
	projection.deleteProjection();
	response.sendRedirect("projections.jsp?mode=search&movie=" + request.getParameter("movie") + 
			"&hall=" + request.getParameter("hall") + "&fromDate=" + request.getParameter("fromDate") + 
			"&fromTime=" + request.getParameter("fromTime") + "&toDate=" + request.getParameter("toDate") + 
			"&toTime=" + request.getParameter("toTime") + "&submit");
}
%>

<div class="content">
	
	<% 	
		// ### Search ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("search")) { 
			
			int movie = request.getParameter("movie")!=null ? Integer.parseInt( request.getParameter("movie")) : 0;
			int hall = request.getParameter("hall")!=null ? Integer.parseInt( request.getParameter("hall")) : 0;
			String fromDate = request.getParameter("fromDate")!=null ? request.getParameter("fromDate") : "";
			String fromTime = request.getParameter("fromTime")!=null ? request.getParameter("fromTime") : "";
			String toDate = request.getParameter("toDate")!=null ? request.getParameter("toDate") : "";
			String toTime = request.getParameter("toTime")!=null ? request.getParameter("toTime") : "";
			Vector<Projection> projections = null;
				
	%>
	
	<h2 id="title">Прожекции</h2>
		
		<form action="projections.jsp?mode=search" method="post" id="search" name="search" onsubmit="return validateProjectionSearch();">
			<table class="formtable">
				<tr>
					<td align="right">Заглавие на филм:</td>
					<td align="left">
						<select id="movie" name="movie">
							<option value="0" <%=movie==0?"selected":""%>>Всички</option>
							<%=UIUtils.printOptions("select CM01_MOVIE_ID, CM01_MOVIE_NAME from TUDB.TM01_MOVIES", movie)%>
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Зала:</td>
					<td align="left">
						<select id="hall" name="hall">
							<option value="0"<%=hall==0?"selected":""%>>Всички</option>
							<%=UIUtils.printOptions("select CH01_HALL_ID, CH01_HALL_NAME from TUDB.TH01_HALLS", hall)%>
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Начало на прожекцията:</td>
					<td align="left">
						<input type="text" id="fromDate" name="fromDate" style="width: 75px;" value="<%=fromDate%>"/>
						<input type="text" id="fromTime" name="fromTime" style="width: 35px;" value="<%=fromTime%>"/> ч.
					</td>
				</tr>
				<tr>
					<td align="right">Край на прожекцията:</td>
					<td align="left">
						<input type="text" id="toDate" name="toDate" style="width: 75px;" value="<%=toDate%>"/>
						<input type="text" id="toTime" name="toTime" style="width: 35px;" value="<%=toTime%>"/> ч.
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Търси"/>&nbsp;<input type="button" id="add" name="add" value="Добави нова" onclick="window.location = 'projections.jsp?mode=add';"/></td>
				</tr>
			</table>
		</form>
		
		
		<% 
			if (request.getParameter("submit")!=null) {
				String formatFrom = fromTime.equals("") ? fromDate + " 00:00:00" : fromDate + " " + fromTime + ":00";
				String formatTo = fromTime.equals("") ? toDate + " 00:00:00" : toDate + " " + toTime + ":00";
				projections = Projection.searchProjections(movie, hall, fromDate.equals("")?null:formatFrom, toDate.equals("")?null:formatTo, 0);
			}
		
			if (projections != null) {
		%>
		<table class="resulttable">
			<tr>
				<th width="200">Филм</th>
				<th width="100">Зала</th>
				<th width="100">Начален час</th>
				<th width="100">Краен час</th>
				<th width="100">Действие</th>
			</tr>
			<%for (int i=0; i<projections.size(); i++) { %>
			<tr>
				<td><%=projections.get(i).getMovie().getMovieName()%></td>
				<td><%=projections.get(i).getHall().getHallName()%></td>
				<td><%=projections.get(i).getProjectionBeginDateTime()%></td>
				<td><%=projections.get(i).getProjectionEndDateTime()%></td>
				<td>
					<a href="projections.jsp?mode=edit&id=<%=projections.get(i).getProjectionId()%>">Редактирай</a>&nbsp;
					<a href="#" 
						onclick="confirmMsg('Сигурни ли сте, че искате да изтриете тази прожекция?','projections.jsp?mode=delete&movie=<%=movie%>&hall=<%=hall%>&fromDate=<%=fromDate%>&fromTime=<%=fromTime%>&toDate=<%=toDate%>&toTime=<%=toTime%>&submit&id=<%=projections.get(i).getProjectionId()%>');">
					Изтрий</a>
				</td>
			</tr>
			<%} %>
		</table>
		<%=projections.size()==0 ? "<div class=\"error\">Няма намерени резултати!</div>":""%>
			<%} %>
		
	<%} %>
		
		
	<% 	
		// ### Edit ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("edit")) { 
			
			int id = Integer.parseInt(request.getParameter("id"));
			
			Projection projection = Projection.searchProjections(0, 0, null, null, id).get(0);
			String fromDate = projection.getProjectionBeginDateTime().substring(0, 10);
			String fromTime = projection.getProjectionBeginDateTime().substring(11, 16);
			String toDate = projection.getProjectionEndDateTime().substring(0, 10);
			String toTime = projection.getProjectionEndDateTime().substring(11, 16);
	%>
		
		<h2 id="title">Редактиране на прожекция</h2>
		
		<form action="projections.jsp?mode=save" method="post" id="edit" name="edit" onsubmit="return validateProjectionDetails();">
			<input type="hidden" id="id" name="id" value="<%=id%>">
			<table class="formtable">
				<tr>
					<td align="right">Заглавие на филм:</td>
					<td align="left">
						<select id="movie" name="movie">
							<%=UIUtils.printOptions("select CM01_MOVIE_ID, CM01_MOVIE_NAME from TUDB.TM01_MOVIES", projection.getMovie().getMovieId())%>
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Зала:</td>
					<td align="left">
						<select id="hall" name="hall">
							<%=UIUtils.printOptions("select CH01_HALL_ID, CH01_HALL_NAME from TUDB.TH01_HALLS", projection.getHall().getHallId())%>
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Начало на прожекцията:</td>
					<td align="left">
						<input type="text" id="fromDate" name="fromDate" style="width: 75px;" value="<%=fromDate%>"/>
						<input type="text" id="fromTime" name="fromTime" style="width: 35px;" value="<%=fromTime%>"/> ч.
					</td>
				</tr>
				<tr>
					<td align="right">Край на прожекцията:</td>
					<td align="left">
						<input type="text" id="toDate" name="toDate" style="width: 75px;" value="<%=toDate%>"/>
						<input type="text" id="toTime" name="toTime" style="width: 35px;" value="<%=toTime%>"/> ч.
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Запази"/>
				</tr>
			</table>
		</form>

		<a href="projections.jsp?mode=search">&lt;&lt; Търсене</a>
	
	<%} %>
	
	
	<% 	
		// ### Add ###
		if (request.getParameter("mode")!=null && request.getParameter("mode").equals("add")) { 
			
	%>
		
		<h2 id="title">Добавяне на прожекция</h2>
		
		<form action="projections.jsp?mode=addproj" method="post" id="edit" name="edit" onsubmit="return validateProjectionDetails();">
			<table class="formtable">
				<tr>
					<td align="right">Заглавие на филм:</td>
					<td align="left">
						<select id="movie" name="movie">
							<%=UIUtils.printOptions("select CM01_MOVIE_ID, CM01_MOVIE_NAME from TUDB.TM01_MOVIES", 0)%>
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Зала:</td>
					<td align="left">
						<select id="hall" name="hall">
							<%=UIUtils.printOptions("select CH01_HALL_ID, CH01_HALL_NAME from TUDB.TH01_HALLS", 0)%>
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Начало на прожекцията:</td>
					<td align="left">
						<input type="text" id="fromDate" name="fromDate" style="width: 75px;"/>
						<input type="text" id="fromTime" name="fromTime" style="width: 35px;"/> ч.
					</td>
				</tr>
				<tr>
					<td align="right">Край на прожекцията:</td>
					<td align="left">
						<input type="text" id="toDate" name="toDate" style="width: 75px;"/>
						<input type="text" id="toTime" name="toTime" style="width: 35px;"/> ч.
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" id="submit" name="submit" value="Добави"/>
				</tr>
			</table>
		</form>

		<a href="projections.jsp?mode=search">&lt;&lt; Търсене</a>
	
	<%} %>
		

</div>

<%@include file="template/footer.jspf" %>